package com.lzh.persevere.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.persevere.mapper.CommentMapper;
import com.lzh.persevere.mapper.DislikeInfoMapper;
import com.lzh.persevere.model.entity.Comment;
import com.lzh.persevere.model.entity.DislikeInfo;
import com.lzh.persevere.model.ro.comment.DislikeCommentRO;
import com.lzh.persevere.service.DislikeInfoService;
import com.lzh.persevere.utils.IpUtil;
import com.lzh.persevere.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 点踩表服务实现类
 *
 * @author luzhiheng
 * @since 2025-07-13
 */
@Service
public class DislikeInfoServiceImpl extends ServiceImpl<DislikeInfoMapper, DislikeInfo> implements DislikeInfoService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Boolean dislikeComment(DislikeCommentRO ro) {
        Long commentId = ro.getCommentId();
        Boolean isDislike = ro.getIsDislike();
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) {
            return false;
        }

        if (isDislike) {
            comment.setDislikeCount(comment.getDislikeCount() + 1);
            commentMapper.updateById(comment);

            DislikeInfo dislikeInfo = new DislikeInfo()
                    .setIp(IpUtil.getIpAddress())
                    .setModuleId(comment.getModuleId())
                    .setDislikeCommentId(commentId);
            return baseMapper.insert(dislikeInfo) > 0;
        } else {
            comment.setDislikeCount(comment.getDislikeCount() - 1);
            commentMapper.updateById(comment);
            baseMapper.deleteByCommentIdAndCreateUserId(commentId, SecurityUtils.getUserId());
            return true;
        }
    }
}
